/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2012 www.pretty-tools.com
 */

wsshWebProfileSerializer.profileExists = function(profile)
{
    if (!profile)
        return false;

    if (yrprxComponentsFactory.implementsIFile(profile))
        return profile.exists();

    return true;
}

wsshWebProfileSerializer.removeProfile = function(profile)
{
    if (profile && profile.exists())
        profile.remove(false);
}

wsshWebProfileSerializer.getProfileDirectory = function()
{
    var profileDirectory = yrprxUtil.getProfileDirectory();

    profileDirectory.append("WebSlideShow");

    try
    {
        if (!profileDirectory.exists())
            profileDirectory.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 00400|00200|00100);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("getProfileDirectory:", ex);
        // reset profile directory to root
        profileDirectory = yrprxUtil.getProfileDirectory();
    }

    return profileDirectory;
}

wsshWebProfileSerializer.getProfile = function(profileName)
{
    var profile = this.getProfileDirectory();

    profile.append(profileName + ".xml");

    return profile;
}

wsshWebProfileSerializer.loadProfileFromFile = function(file)
{
    try
    {
        if (!file)
        {
            var fp = yrprxComponentsFactory.createFilePicker();
            // init file picker
            fp.init(window, wsshResources["webslideshow.filepicker.import.rules"], Components.interfaces.nsIFilePicker.modeOpen);
            fp.appendFilter(wsshResources["webslideshow.filepicker.filter.xml"], "*.xml");
            // open file picker
            if (fp.show() == Components.interfaces.nsIFilePicker.returnOK)
                file = fp.file;
        }

        return this.load(file);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("loadProfileFromFile:", ex);
    }
    return new Array();
}

wsshWebProfileSerializer.exportUserProfile = function(items)
{
    try
    {
        var fp = yrprxComponentsFactory.createFilePicker();
        // init file picker
        fp.init(window, wsshResources["webslideshow.filepicker.export.rules"], Components.interfaces.nsIFilePicker.modeSave);
        fp.appendFilter(wsshResources["webslideshow.filepicker.filter.xml"], "*.xml");
        fp.defaultString = "WebSlideShowRules.xml";
        fp.defaultExtension = "xml";
        // open file picker
        if (fp.show() == Components.interfaces.nsIFilePicker.returnCancel)
            return;

        this.save(items, fp.file);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("exportUserProfile:", ex);
    }
}
